﻿using System.Windows;
using System.Windows.Controls;

namespace Gavel.Common.Component.Extension;

/// <summary>
/// 元数据扩展类
/// </summary>
public static class MetadataExtension
{
    /// <summary>
    /// 绑定默认样式
    /// </summary>
    /// <param name="property">表示可以通过诸如样式、数据绑定、动画和继承等方法设置的属性</param>
    /// <typeparam name="T">控件泛型</typeparam>
    public static void OverrideDefaultStyle<T>(this DependencyProperty property) => property.OverrideMetadata(typeof(T), new FrameworkPropertyMetadata(typeof(T)));

    /// <summary>
    /// 绑定按钮点击事件
    /// </summary>
    /// <param name="button">按钮</param>
    /// <param name="handler">点击事件</param>
    public static void AttachButtonClickEvent(this Button button, RoutedEventHandler handler) => button.Click += handler;

    /// <summary>
    /// 使用指定的属性名称、属性类型和所有者类型注册依赖项属性
    /// </summary>
    /// <param name="property">属性名称</param>
    /// <typeparam name="TType">目标类型泛型</typeparam>
    /// <typeparam name="TProperty">属性泛型</typeparam>
    /// <returns>依赖项属性标识符，应用于设置类中公共静态只读字段的值</returns>
    public static DependencyProperty Register<TType, TProperty>(this string property) =>
        DependencyProperty.Register(property.Replace("Property", string.Empty), typeof(TProperty), typeof(TType));

    /// <summary>
    /// 使用指定的属性名称、属性类型和所有者类型注册依赖项属性
    /// </summary>
    /// <param name="property">属性名称</param>
    /// <param name="value">默认值</param>
    /// <typeparam name="TType">目标类型泛型</typeparam>
    /// <typeparam name="TProperty">属性泛型</typeparam>
    /// <returns>依赖项属性标识符，应用于设置类中公共静态只读字段的值</returns>
    public static DependencyProperty Register<TType, TProperty>(this string property, TProperty? value) =>
        value is null
            ? DependencyProperty.Register(property.Replace("Property", string.Empty), typeof(TProperty), typeof(TType))
            : DependencyProperty.Register(property.Replace("Property", string.Empty), typeof(TProperty), typeof(TType), new FrameworkPropertyMetadata(value));

    /// <summary>
    /// 使用指定的属性名称、属性类型和所有者类型注册依赖项属性
    /// </summary>
    /// <param name="property">属性名称</param>
    /// <param name="metadata">报告或应用依赖属性的元数据，特别是添加特定于框架的属性系统特征</param>
    /// <typeparam name="TType">目标类型泛型</typeparam>
    /// <typeparam name="TProperty">属性泛型</typeparam>
    /// <returns>依赖项属性标识符，应用于设置类中公共静态只读字段的值</returns>
    public static DependencyProperty Register<TType, TProperty>(this string property, FrameworkPropertyMetadata metadata) =>
        DependencyProperty.Register(property.Replace("Property", string.Empty), typeof(TProperty), typeof(TType), metadata);

    /// <summary>
    /// 使用指定的属性名称、属性类型和所有者类型注册依赖项属性
    /// </summary>
    /// <param name="property">属性名称</param>
    /// <param name="metadata">报告或应用依赖属性的元数据，特别是添加特定于框架的属性系统特征</param>
    /// <typeparam name="TType">目标类型泛型</typeparam>
    /// <typeparam name="TProperty">属性泛型</typeparam>
    /// <returns>定义依赖项属性在应用于特定类型时的某些行为方面，包括它所注册的条件</returns>
    public static DependencyProperty Register<TType, TProperty>(this string property, PropertyMetadata metadata) =>
        DependencyProperty.Register(property.Replace("Property", string.Empty), typeof(TProperty), typeof(TType), metadata);
}